package com.example.content.viewmodule.me

import android.util.Log
import com.example.content.DO_ACTION
import com.example.content.USER_LIKE
import com.example.content.manager.GlobalMgr
import com.example.content.repository.RepositoryMgr
import com.example.content.repository.bean.*
import com.example.content.repository.files.FileConvert
import com.example.content.viewmodule.BaseViewModel
import com.example.content.viewmodule.login.TokenStore
import com.example.content.viewmodule.viewdata.CommonViewState
import com.example.core.livedata.SingleLiveEvent
import com.example.core.network.body.ProgressResponseCallback

/**
 * Created by zhudongao
 * Desc  关注、粉丝页面的viewmodel
 * Date 2020/11/7
 */
class MyPageCommonViewModel : BaseViewModel<CommonViewState>() {

    val followersData = SingleLiveEvent<List<UserInfo>>()
    val responseResult = SingleLiveEvent<Boolean>()
    val productResult = SingleLiveEvent<List<ProductInfo>>()
    val collectResult = SingleLiveEvent<List<ProductCollectInfo>>()
    val rewardResult = SingleLiveEvent<List<UserRewardResp>>()

    fun getFollowers(action: Int) {
        launch(
            block = {
                val followers = FollowersInfoReq(action = action, user_id = TokenStore.userId)
                followersData.postValue(RepositoryMgr.getFollowers(followers))
            }
        )
    }

    fun getUserInfo() {
        launch(
            block = {
                val req = UserInfoReq(id_list = listOf(TokenStore.userId))
                val apiData = RepositoryMgr.getUserInfo(req)
                if (apiData.code == 200) {
                    followersData.postValue(apiData.apiData())
                }
            }
        )
    }

    /**
     * 文件上传
     */
    fun uploadFile(files:List<String>){
        launch(
            block = {
                RepositoryMgr.uploadFile(
                    FileConvert.fileWrappers(
                        files,
                        object : ProgressResponseCallback {
                            override fun onResponseProgress(
                                bytesWritten: Long,
                                contentLength: Long,
                                finish: Boolean
                            ) {

                            }
                        })
                )
            }
        )
    }

    fun queryUserProduct(pageType: Int, userId:Int) {
        getProductListInfo(userId, pageType)
    }

    //获取用户打赏记录
    private fun getUserRewardInfo(id: Int, offset: Int) {
        launch(
            block = {
                rewardResult.postValue(RepositoryMgr.getUserRewardInfo(id, offset).apiData())
                responseResult.postValue(true)
            }
        )
    }

    //获取用户作品信息
    private fun getProductListInfo(id: Int, type: Int) {
        launch(
            block = {
//                if (type == USER_LIKE) {
//                    productResult.postValue(
//                        RepositoryMgr.getUserCollect(TokenStore.userId, 0).apiData()
//                    )
//                } else {
//                    productResult.postValue(RepositoryMgr.getUserProducts(id, type).apiData())
//                }
                productResult.value = RepositoryMgr.getUserProducts(id, type).apiData()
                responseResult.value = true
            }
        )
    }

//    private fun getUserCollects(id: Int, offset: Int) {
//        launch(
//            block = {
//                collectResult.postValue(RepositoryMgr.getUserCollect(id, offset).apiData())
//                responseResult.postValue(true)
//            }
//        )
//    }

    fun modifyUserInfo(req:ModifyUserInfoReq, files: List<String>) {
        launch(
            block = {
                val imageFile = files[0]
                if (imageFile.isNotBlank()) {
                    if (imageFile.startsWith("/api/")) {
                        req.avatar = imageFile
                    } else {
                        val images = RepositoryMgr.uploadFile(FileConvert.fileWrappers(files, null))
                        req.avatar = images?.get(0) ?: ""
                    }
                }
                val result = RepositoryMgr.modifyUserInfo(req)
                if (result.code == 200) {
                    responseResult.postValue(true)
                    GlobalMgr.userInfo.postValue(result.apiData())
                } else {
                    responseResult.postValue(false)
                }
            }
        )
    }

    fun feedback(desc:String, photoList:List<String>) {
        launch(
            block = {
                val feedReq = FeedbackReq(desc, photoList, TokenStore.userId)
                val code = RepositoryMgr.feedBack(feedReq)
                if (code == 200) {
                    responseResult.postValue(true)
                } else {
                    responseResult.postValue(false)
                }
            }
        )
    }

    /**
     * 关注、取消关注
     */
    fun follow(followReq: FollowReq) {
        launch(
            block = {
                val result = RepositoryMgr.follow(followReq)
                GlobalMgr.follow.postValue(result.apiData())
            }
        )
    }

    /**
     * 点赞, 收藏
     */
    fun userAction(userActionReq: UserActionReq) {
        launch(
            block = {
                Log.w("testidddd", "调用次数：")
                val result = RepositoryMgr.userAction(userActionReq)
                if (result.code == 200) {
                    GlobalMgr.collect.postValue(
                        Triple(
                            userActionReq.creation_id,
                            userActionReq.action_type == DO_ACTION,
                            System.currentTimeMillis()
                        )
                    )
                    GlobalMgr.thumbUp.postValue(true)
                }
            }
        )
    }
}